<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/1
 * Time: 10:08
 */
namespace app\admin\controller;

use app\admin\model\OpinionModel;
use app\admin\model\UserModel;
use app\common\model\ImageModel;
use app\common\model\ReplyModel;
use wechat\wechatCallbackapiTest;

class Opinion extends Base
{
    public function index()
    {
        $condition_type =  $this->request->param('condition_type');
        $condition_val = $this->request->param('condition_val');
        //条件
        $condition = [
            'condition_type'=>$condition_type??'',
            'condition_val'=>$condition_val??'',
        ];

        if($condition_type){
            switch($condition_type){
                case 'project_id':
                    $data = OpinionModel::order('created_at', 'desc')
                        ->where('project_id','like',"%$condition_val%")
                        ->paginate(10);
                    break;
                case 'tel':
                    $data = OpinionModel::order('created_at', 'desc')
                        ->where('tel',$condition_val)
                        ->paginate(10);
                    break;
            }

        }else{
            $data = OpinionModel::order('created_at', 'desc')->paginate(10);
        }



        $arr = [];
        //项目反馈类型
        $xiangmu_op = feedback_type_1();
        //整体反馈
        $all_op = feedback_type_2();
        if($data){
            foreach ($data as $k => $v) {
                $user = UserModel::find($v->user_id);
                //当此 用户存在则显示
                //查看是否回复
                $reply = ReplyModel::where('opinion_id', $v->id)->select();
                $data[$k]->reply = $reply;
                //图片
                $re = ImageModel::all($v->src);
                $src_all = [];
                foreach ($re as $r => $y) {
                    $src_all[] = $y->src;
                }
                $data[$k]->srcs = $src_all;
                //用户
                $data[$k]->user_name = $user['user_name'];
                if ($v->project_id) {
                    $data[$k]->title_name = $xiangmu_op[$data[$k]->title];
                } else {
                    $data[$k]->title_name = $all_op[$data[$k]->title];
                }
            }
        }

        $this->assign('condition', $condition);
        $this->assign('data', $data);
        $this->assign('page', $data->render());
        return view('index');
    }

    public function del()
    {
        $re = OpinionModel::destroy($_POST['opinion_id']);

        echo $re;
    }

    //回复
    public function reply($id)
    {
        $item = OpinionModel::get($id);
        $user = UserModel::get($item->user_id);

        if ($item->type == '1') {
            //项目反馈
            $re = feedback_type_1();
            //原因
            $title = $re[$item->title];
        } elseif ($item->type == '2') {
            //普通反馈
            $re = feedback_type_2();
            //原因
            $title = $re[$item->title];
        }

        $this->assign('user_name', $user->user_name);
        $this->assign('title', $title);
        $this->assign('item', $item);
        return view('add_reply');
    }

    //回复表单接受
    public function reply_form()
    {
        if (request()->isPost()) {
            $re = ReplyModel::create([
                'opinion_id' => $_POST['opinion_id'],
                'content' => $_POST['content'],
                'created_at' => time(),
                'is_look' => '0'
            ]);
            if ($re) {
                $this->tel_send($_POST['opinion_id']);
                return redirect('admin/opinion/index');
            } else {
                $this->error('回复失败');
            }
        } else {
            echo '非法请求';
        }
    }

    //修改
    public function edit($id)
    {

        //查询反馈信息
        $item = OpinionModel::get($id);
        //反馈 用户信息
        $user = UserModel::get($item->user_id);
        if(!$user){
            $this->error('没有找到该用户');
        }
        if ($item->type == '1') {
            //项目反馈
            $re = feedback_type_1();
            //原因
            $title = $re[$item->title];
        } elseif ($item->type == '2') {
            //普通反馈
            $re = feedback_type_2();
            //原因
            $title = $re[$item->title];
        }
        //原因
        //查询回复信息
        $reply = ReplyModel::where('opinion_id', $id)->find();
        $this->assign('reply', $reply);
        $this->assign('user_name', $user->user_name);
        $this->assign('title', $title);
        $this->assign('item', $item);
        return view('edit_reply');
    }

    //修改表单
    public function update()
    {

        if (request()->isPost()) {

            $re = ReplyModel::where('opinion_id', $_POST['opinion_id'])->update([
                'content' => $_POST['content']
            ]);
            if ($re) {
                ReplyModel::where('opinion_id', $_POST['opinion_id'])->update([
                    'is_look' => '0',
                    'created_at' => time()
                ]);
                $this->tel_send($_POST['opinion_id']);


                $this->success('修改并发送成功', 'admin/opinion/index');
            } else {
                $this->error('修改并发送失败');
            }
        } else {
            $this->error('非法请求');
        }
    }
    //回复模板
    public function tel_send($opinion_id){
        $reply = ReplyModel::where('opinion_id',$opinion_id)->find();
        $opinion = OpinionModel::find($opinion_id);
        //更新发送状态
        ReplyModel::where('id', $reply->id)->update([
            'is_send' => '1'
        ]);
        //反馈类型
        if($opinion->type == 1){
            //项目反馈
            $type =feedback_type_1();
            $address = mb_substr($opinion->project_id,0,mb_strlen($opinion->project_id)-2);
            $type = $type[$opinion->title]."--".$address.'*号';
        }else{
            //2统一反馈
            $type =feedback_type_2();
            $type = $type[$opinion->title];
        }
        $url = urldecode('http://'. $_SERVER['SERVER_NAME'] .'/index/message/index.html');
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc7f82feeff1f94fb&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state=diyadaikuan#wechat_redirect";
        $user = UserModel::find($opinion->user_id);
        if($user && $reply && $opinion){
            $wechat =new wechatCallbackapiTest();

            $data = '{
           "touser":"'.$user->openid.'",
           "template_id":"XVxiDzffpgIr1_pgHsMdqPx5c7lHnoSZEREHBxqJHl4",
           "url":"'.$url.'",
           "data":{
                   "first": {
                       "value":"尊敬的'."$user->user_name".'",
                       "color":"#173177"
                   },
                   "keyword1":{
                       "value":"'.$type.'",
                       "color":"#173177"
                   },
                   "keyword2": {
                       "value":"'.$reply->content.'",
                       "color":"#173177"
                   },
                   "remark":{
                       "value":"感谢您的使用，祝您生活愉快！",
                       "color":"#173177"
                   }
           }
       }';
            $wechat->tpl_send($data);
        }else{
            $this->error('没有获取到该用户');
        }

    }
}